package com.tiger.jms.sample1;

import javax.naming.*;
import javax.jms.*;

public class Subscriber {
	public static void main(String[] args) {
		try {
			InitialContext ctx = new InitialContext();
			TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx.lookup("ConnectionFactory");
			TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
			TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
			Topic topic = (Topic) ctx.lookup("topic/sampleTopic");
			TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);

			topicConnection.start();
			Message message = topicSubscriber.receive();
			if (message instanceof TextMessage) {
				TextMessage textMessage = (TextMessage) message;
				System.out.println("Message : " + textMessage.getText());
			} else {
				System.out.println("不明なメッセージ種別です");
			}

			topicSubscriber.close();
			topicSession.close();
			topicConnection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
